package cn.tedu.tmall.admin.mall.service.impl;

import cn.tedu.tmall.admin.mall.dao.repository.IGoodsDetailRepository;
import cn.tedu.tmall.admin.mall.dao.repository.IGoodsRepository;
import cn.tedu.tmall.admin.mall.dao.repository.IMallCategoryRepository;
import cn.tedu.tmall.admin.mall.pojo.param.GoodsAddParam;
import cn.tedu.tmall.admin.mall.pojo.po.GoodsDetailPO;
import cn.tedu.tmall.admin.mall.pojo.po.GoodsPO;
import cn.tedu.tmall.admin.mall.pojo.po.MallCategoryPO;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsListVO;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsVO;
import cn.tedu.tmall.admin.mall.service.IGoodsService;
import cn.tedu.tmall.common.web.JsonResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class GoodsService implements IGoodsService {
    @Autowired
    private IGoodsRepository goodsRepository;
    @Autowired
    private IMallCategoryRepository mallCategoryRepository;
    @Autowired
    private IGoodsDetailRepository goodsDetailRepository;


    @Override
    public void addNewGoods(GoodsAddParam goodsAddParam) {
        MallCategoryPO mallCategoryPO = mallCategoryRepository.selectById(goodsAddParam.getCategoryId());
        if (mallCategoryPO != null) {
            if (mallCategoryPO.getEnable() != 0) {
                GoodsPO goodsPO = new GoodsPO();
                BeanUtils.copyProperties(goodsAddParam, goodsPO);
                goodsPO.setCategoryName(mallCategoryPO.getName());
                Integer goodCount = goodsRepository.save(goodsPO);
                if (goodCount != 1) {
                    throw new RuntimeException("发布商品失败");
                }
                GoodsDetailPO goodsDetailPO = new GoodsDetailPO();
                BeanUtils.copyProperties(goodsAddParam, goodsDetailPO);
                goodsDetailPO.setId(goodsPO.getId());
                Integer goodDetailCount = goodsDetailRepository.save(goodsDetailPO);
                if (goodDetailCount != 1) {
                    throw new RuntimeException("发布商品失败");
                }
            } else {
                throw new RuntimeException("商品分类已禁用，发布商品失败");
            }
        } else {
            throw new RuntimeException("商品分类不存在");
        }
    }

    @Override
    public List<GoodsListVO> goodsList(Long categoryId, Integer pageCount, Integer pageSize) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("category_id", categoryId);
        return listPO2VO(goodsRepository.selectAll(queryWrapper, pageCount, pageSize));
    }

    @Override
    public GoodsVO selectDetail(Long id) {
        GoodsPO goodsPO = goodsRepository.selectById(id);
        if (goodsPO == null) {
            throw new RuntimeException("商品不存在");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("goods_id", id);
        GoodsDetailPO goodsDetailPO = goodsDetailRepository.selectByGoodId(queryWrapper);
        if (goodsDetailPO == null) {
            throw new RuntimeException("商品不存在");
        }
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyProperties(goodsPO, goodsVO);
        BeanUtils.copyProperties(goodsDetailPO, goodsVO);
        return goodsVO;
    }
    private List<GoodsListVO> listPO2VO(List<GoodsPO> pos) {
        List<GoodsListVO> listVOS = new ArrayList<>();
        for (GoodsPO goodsPO:pos) {
            GoodsListVO goodsListVO = new GoodsListVO();
            BeanUtils.copyProperties(goodsPO, goodsListVO);
            listVOS.add(goodsListVO);
        }
        return listVOS;
    }
}
